package common

import (
	"context"
	"fmt"
	"os"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"

	"clockIn/api/common/v1"
)

func (c *ControllerV1) UploadPicture(ctx context.Context, req *v1.UploadPictureReq) (res *v1.UploadPictureRes, err error) {
	if req.File == nil {
		return nil, gerror.NewCode(gcode.CodeMissingParameter, "请上传文件")
	}
	_, mErr := os.Stat("./static")
	if mErr != nil {
		mErr = os.Mkdir("./static", os.ModePerm)
		if mErr != nil {
			fmt.Println("文件夹创建失败", mErr)
			return
		}
	}
	folder := "./static/"
	os.Mkdir(folder, os.ModePerm)
	req.File.Save(folder)
	res = new(v1.UploadPictureRes)
	res.Url = "/static/" + req.File.Filename
	return
}
